//
//  ZJComposeToolBarView.swift
//  Webo
//
//  Created by 曾静 on 16/7/3.
//  Copyright © 2016年 qq. All rights reserved.
//

import UIKit
//创建按钮的tag的枚举值,方便阅读
enum ComposeTagType:Int{
    
    case Picture = 10
    
    case Mention = 20
    
    case Trend = 30
    
    case Emotion = 40
    
    case Add = 50
}


class ZJComposeToolBarView: UIView {
    
    //创建一个闭包,按钮点击的时候通过传入不同的tag值,是控制器做出不同的相应,可选类型创建为var的类型
    var clourse:((tagType:ComposeTagType)->())?

    
    var isEmotion:Bool = false{
        
        
        didSet{
            
            //
            if !isEmotion {
               
                //图片为键盘
                //取出笑脸按钮
                let hahaButton:UIButton = self.btnArray[3]
                
                hahaButton.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                hahaButton.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
                
            }else{
                //图片为笑脸
                 let hahaButton:UIButton = self.btnArray[3]
                hahaButton.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                hahaButton.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
            
            
        }
    }
    //创建空的数组
   private var btnArray:[UIButton] = [UIButton]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置视图
    private func setupUI(){
        
        //创建一个btn的数组
        for i in 0...4{
            
            let arr:[String] = ["compose_toolbar_picture","compose_mentionbutton_background", "compose_trendbutton_background", "compose_emoticonbutton_background","compose_add_background"]
            
            
            //创建数组type:CZComposeToolbarType(rawValue: button.tag)!
            self.btnArray.append(self.addToolBarButtons(arr[i], btnTag: ComposeTagType(rawValue: 10 + i * 10)!))
            //添加约束
           
        }
        
        //添加约束,jiaquan
        for i in 0...4{
        self.btnArray[i].snp_makeConstraints { (make) -> Void in

            if i == 0{
                make.top.leading.bottom.equalTo(self)
                make.width.equalTo(btnArray[1])
            }else if i == 4{
                make.top.trailing.bottom.equalTo(self)
                make.leading.equalTo(btnArray[3].snp_trailing)
            }else{
                make.top.bottom.equalTo(self)
                make.leading.equalTo(btnArray[i - 1].snp_trailing)
                make.width.equalTo(btnArray[i + 1])
            }
            
            
        }
 
    }
    
        
    }
    
    
    func addToolBarButtons(imageName:String, btnTag:ComposeTagType)->UIButton{
        
        let btn = UIButton()
        
        //添加tag
        btn.tag = btnTag.rawValue
        
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        
        //由于只有一张,设置同一个相当于取消高亮
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        
        
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
         btn.addTarget(self, action: "toolBarButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        //创建完成之后记得添加视图
        addSubview(btn)
        
        return btn
        
        
    }
    //按钮的点击事件,通过传入tag值来区分按钮,按钮的点击事件传进btn自身
    
    func toolBarButtonClick(button:UIButton){
        
        //判断闭包是否实现记得加?,相当于block != nil
        clourse?(tagType: ComposeTagType(rawValue: button.tag)!)
    }
}
